package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mwj
 * @date 2021/11/24
 * @TIME:15:43
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    //todo 还需理解
    @Override
    public void saveSetmealDto(SetmealDto setmealDto) {
        //1.保存套餐数据到setmeal表中
        //新增完成够，套餐id就存在了
        this.save(setmealDto);
        // 2.保存套餐菜品表 setmealDishes
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(c -> {
            //2.1为每个套餐菜品设置套餐id
            c.setSetmealId(setmealDto.getId());
            return c;
        }).collect(Collectors.toList());
        //2.2写入到数据库
        setmealDishService.saveBatch(setmealDishes);

//        this.save(setmealDto);
//
//        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
//
//        for (SetmealDish setmealDish : setmealDishes) {
//            setmealDish.setSetmealId(setmealDto.getId());
//        }
//
//        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //分页查询
        IPage<Setmeal> ipage = new Page<>(page, pageSize);
        //如果有条件就查询
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if (name != null) {
            wrapper.eq(Setmeal::getName, name);
        }
        //去掉逻辑删除的
        wrapper.eq(Setmeal::getIsDeleted, 0);
        IPage<Setmeal> page1 = this.page(ipage, wrapper);
        //再次封装categoryname属性
        List<Setmeal> setmeals = page1.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();
        //遍历所有的套餐对象，转变成一个套餐dto对象，并且查询出分类名称
        for (Setmeal setmeal : setmeals) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            //查询分类名字
            Category category = categoryService.getById(setmeal.getCategoryId());
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            setmealDtos.add(setmealDto);
        }
        //返回查询到的集合
        Page<SetmealDto> page2 = new Page<>();
        page2.setRecords(setmealDtos);
        page2.setTotal(page1.getTotal());
        return page2;
    }

    @Override
    public void deleteIds(Long[] ids) {
        //修改套餐的isdelete
        //方法二
//        LambdaUpdateWrapper<Setmeal> setmealWrapper=new LambdaUpdateWrapper<>();
//        setmealWrapper.in(Setmeal::getId,Arrays.asList(ids))
//        .set(Setmeal::getIsDeleted,1);
//        this.update(setmealWrapper);
//
//        LambdaUpdateWrapper<SetmealDish> setmealDishWrapper=new LambdaUpdateWrapper<>();
//        setmealDishWrapper.in(SetmealDish::getSetmealId,Arrays.asList(ids))
//        .set(SetmealDish::getIsDeleted,1);
//        setmealDishService.update(setmealDishWrapper);

        //方法一
        //1.修改所有ids的isdelete字段为1  代表已经被逻辑删除
        //1.1查询出所有ids的套餐信息
        List<Setmeal> setmeals = this.listByIds(Arrays.asList(ids));
        //1.2循环修改所有套餐的isdelete属性
        setmeals = setmeals.stream().map(c -> {
            c.setIsDeleted(1);
            return c;
        }).collect(Collectors.toList());
        //1.3只需要修改
        this.updateBatchById(setmeals);
        //修改套餐菜品的isdelete
        for (Setmeal setmeal : setmeals) {
            //查询出套餐的菜品信息
            Long setmealId = setmeal.getId();
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId, setmealId);
            List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);

            setmealDishes = setmealDishes.stream().map(c -> {
                c.setIsDeleted(1);
                return c;
            }).collect(Collectors.toList());
            setmealDishService.updateBatchById(setmealDishes);
        }


    }

    @Override
    public void statusByIds(Integer statusHandle, Long[] ids) {
        //先根据ids获得
        List<Setmeal> setmeals = this.listByIds(Arrays.asList(ids));
        //再遍历改变status
        setmeals = setmeals.stream().map(c -> {
            c.setStatus(statusHandle);
            return c;
        }).collect(Collectors.toList());
        //修改
        this.updateBatchById(setmeals);
    }

    @Override
    public SetmealDto findById(Long id) {
        //先获取setmeal
        Setmeal setmeal = this.getById(id);
        //通过setmealid查询出setmealdishes
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        //再复制给setmealDto
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //setmeal修改，setmealdish先删除后新增
        //setmeal修改
        this.updateById(setmealDto);
        //setmealdish先删除后新增
        //1.根据setmeal的id查出setmealdish并删除
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        //todo 是否需要逻辑删
        setmealDishService.remove(wrapper);
        //2.添加新的setmealdish
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(c -> {
            c.setSetmealId(setmealDto.getId());
            return c;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    public List<Setmeal> findList(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
        if(setmeal.getCategoryId()!=null){
            wrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        }
        wrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> list = this.list(wrapper);

        return list;
    }
}
